package com.seven.commons;

import com.google.common.base.Charsets;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;

import java.util.List;

/**
 * @工程名:common-utils
 * @作者: sevenDay
 * @时间: 2019/5/19 17:45
 * @描述: 第二种单利模式
 * private static SevenBloomFilterUtils sevenBloomFilterUtils = new SevenBloomFilterUtils<>();
 *
 *      private SevenBloomFilterUtils(){
 *
 *      }
 *      public static SevenBloomFilterUtils getInstance(){
 *          return  sevenBloomFilterUtils;
 *      }
 */
public class SevenBloomFilter {

    private volatile BloomFilter bloomFilter;

    /**
     * 枚举class构造线程安全的类
     */
    private enum BloomFilterSingleton {
        singletonFactory;
        private SevenBloomFilter instance;

        BloomFilterSingleton() {
            instance = new SevenBloomFilter();
        }

        public SevenBloomFilter getInstance() {
            return instance;
        }
    }

    public static SevenBloomFilter getInstance() {
        return BloomFilterSingleton.singletonFactory.getInstance();
    }

    /**
     * 创建一定规则的布隆过滤器
     * @param count
     * @param ffid
     * @return
     */
    private synchronized BloomFilter getBloomFactory(int count,double ffid){

        if (bloomFilter==null){
                bloomFilter = BloomFilter.create(Funnels.stringFunnel(Charsets.UTF_8), count);
                return bloomFilter;
        }else{
            return bloomFilter;
        }
    }

    /**
     * 是否包含元素
     * @param bloomFilter
     * @param key
     * @return
     */
    private boolean mightContain(BloomFilter bloomFilter,String key){
        return bloomFilter.mightContain(key);
    }

    /**
     * 添加
     * @param list
     */
    private void  addElement(List<Object> list){
        list.stream().forEach(x->bloomFilter.put(x));
    }

}
